import {
  createStore,
  findStoreMany,
  deleteStore,
  updateStore,
  createRecord,
  findRecordMany,
  deleteRecord,
  updateRecord
} from "../services/experiment";

export default {

  namespace: 'experiment',

  state: {},

  // subscriptions: {
  //   setup({ dispatch, history }) {  // eslint-disable-line
  //   },
  // },

  effects: {
    * createRecord({payload}, {call, put}) {
      const response = yield call(createRecord, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {experimentRecords: []}});
      return response;
    },
    * findRecordMany({payload}, {call, put}) {
      const response = yield call(findRecordMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {experimentRecords: result}});
      return response;
    },
    * deleteRecord({payload}, {call, put}) {
      const response = yield call(deleteRecord, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {experimentRecords: []}});
      return response;
    },
    * updateRecord({payload}, {call, put}) {
      const response = yield call(updateRecord, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {experimentRecords: []}});
      return response;
    },
    * createStore({payload}, {call, put}) {
      const response = yield call(createStore, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {StoreList: []}});
      return response;
    },

    * findStoreMany({payload}, {call, put}) {
      const response = yield call(findStoreMany, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }

      yield put({type: 'onDataLoad', payload: {experimentStore: result}});
      return response;
    },
    * updateStore({payload}, {call, put}) {
      const response = yield call(updateStore, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {StoreList: []}});
      return response;
    },
    * deleteStore({payload}, {call, put}) {
      const response = yield call(deleteStore, payload);
      if (!response) {
        yield put({type: 'onNotify', payload: {message: "无服务！", type: 'error'}});
        return;
      }
      const {result, status, message} = response;
      if (status !== "ok") {
        yield put({type: 'onNotify', payload: {message, type: 'error'}});
        return;
      }
      yield put({type: 'onDataLoad', payload: {StoreList: []}});
      return response;
    }


  },

  reducers: {
    onDataLoad(state, {payload}) {
      state._notify = undefined;
      return {...state, ...payload};
    }
  }

};
